package com.sunday.scoremanager.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.common.widgets.NoScrollGridView;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.base.BaseLazyFragment;
import com.sunday.scoremanager.entity.Member;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sunday.scoremanager.base.Constants.TEST_URL;

/**
 * Created by 刘涛 on 2017/10/26.
 */

public class MineFragment extends BaseLazyFragment {


    @Bind(R.id.btn_setting)
    ImageView btnSetting;
    @Bind(R.id.user_logo)
    CircleImageView userLogo;
    @Bind(R.id.logo_layout)
    RelativeLayout logoLayout;
    @Bind(R.id.btn_edit)
    ImageView btnEdit;
    @Bind(R.id.user_mobile)
    TextView userMobile;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.product_manage)
    TextView productManage;
    @Bind(R.id.comment_manage)
    TextView commentManage;
    @Bind(R.id.staff_manage)
    TextView staffManage;
    @Bind(R.id.shop_list)
    TextView shopList;

    private Member member;

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_mine, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        if (!isPrepared || !isVisible) {
            return;
        }
//        initRefreshLayout();
//        isPrepared = false;
//        memberId= BaseApplication.getInstance().getMember().getId();
//        ptrFrame.autoRefresh();
        shopList.setVisibility(BaseApp.getInstance().getMember().getPermission() == 1 ? View.VISIBLE : View.GONE);

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        member = BaseApp.getInstance().getMember();
        initView();
    }

    private void initView() {
        Glide.with(mContext)
                .load(member.getLogo())
                .into(userLogo);
        userMobile.setText(member.getAccount());
        userName.setText(member.getShopName());


    }

    @OnClick({R.id.product_manage, R.id.comment_manage, R.id.staff_manage, R.id.btn_setting,R.id.shop_list})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.product_manage:
                intent = new Intent(mContext, ProductsManageActivity.class);
                break;
            case R.id.comment_manage:
                intent = new Intent(mContext, CommentManageActivity.class);
                break;
            case R.id.staff_manage:
                intent = new Intent(mContext, StaffManageActivity.class);
                break;
            case R.id.btn_setting:
                intent = new Intent(mContext, SettingActivity.class);
                break;
            case R.id.shop_list:
                intent = new Intent(mContext, ShopListActivity.class);
                intent.putExtra("fromMine",true);
                break;
        }
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        member = BaseApp.getInstance().getMember();
        initView();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
